#include <graphics.h>
#include <string.h>
#include <alloc.h>
#include "user.h"
#include "menu.h"
#include "bit8.h"
#include "message.h"

#define MaxMsgStrings 20
#define MaxMsgColomns 80
static char **InfoWin;
static int x0,y0,nx,ny;
static int sign=0;

void MessageInit(int ini) {
   if(sign==ini) return;
   if(sign==0) {
      InfoWin=new char*[MaxMsgStrings];
      for(int i=0; i<MaxMsgStrings; i++) {
	 InfoWin[i]=new char[MaxMsgColomns+1];
	 strcpy(InfoWin[i]," ");
      }
   }
   if(sign>0) MessageClear();
   if(mwMaxColomns>MaxMsgColomns) mwMaxColomns=MaxMsgColomns;
   if(mwMaxStrings>MaxMsgStrings) mwMaxStrings=MaxMsgStrings;
   x0=mwLeft*SymbolHorSize;
   y0=mwAbove*SymbolVerSize;
   nx = mwMaxColomns*SymbolHorSize;
   ny = mwMaxStrings*SymbolVerSize;
   if(ini==1) {
      Message(mwMaxStrings-2,"  Output - Undefined");
      Message(mwMaxStrings-5,"  Coding - Undefined");
      Message(1,"  DOE - Undefined");
      MessageOut();
   }
   sign=ini;
}

void MessageClear() {
   setfillstyle( SOLID_FILL,EGA_CYAN);
   bar(x0,y0,x0+nx,y0+ny);

   for(int i=0; i<mwMaxStrings; i++) strcpy(InfoWin[i]," ");
   //delete InfoWin[i];
   //delete InfoWin;
   sign=-1;
}

void Message(int index, char* str){
   if(index<mwMaxStrings) {
      strncpy(InfoWin[index],str, mwMaxColomns);
      (InfoWin[index])[mwMaxColomns]=0;
   }
}

void MessageOut() {
   setfillstyle( SOLID_FILL,EGA_LIGHTGRAY);
   bar(x0,y0,x0+nx,y0+ny);
//   setcolor(EGA_LIGHTGRAY);
   setcolor(EGA_DARKGRAY);
   for(int i=0; i<mwMaxStrings; i++)
	  text(mwLeft, mwAbove+i, InfoWin[i]);
}

void MessageNewDOE() { Message(0,"  New Mask Definition"); }
void MessageDrawDOE() {
   Message(0,"      Viewing  Mask ");
   MessageOut();
}

void MessageName(char* name) {
   sprintf(TmpStr,"  DOE - %s",name);
   Message(1,TmpStr);
   InpFile=ftDoe;
   strcpy(DoeTitle,name);
}

void MessagePar(int ParIndex, char* str) {
   if(ParIndex>0 && ParIndex<mwMaxStrings-8)
      Message(ParIndex+2,str);
}
int MessageSize(double hor, double ver, double s) {
  unsigned int ih=pixel_number_by_step(hor, s);
  unsigned int iv=pixel_number_by_step(ver, s);
  unsigned int MaxLength=16000;
  if(ih>MaxLength || iv>MaxLength) {
     strbox("Large Dimensions or Little Step");
     return 1;
  }
   DoeStep=s;
   DoeWidth=hor;
   DoeHight=ver;
   for(int i=0; i<mwMaxStrings-7; i++) strcpy(InfoWin[i]," ");
   sprintf(TmpStr,"  size = [%d,%d]",iv,ih);
//  Message(mwMaxStrings-7,TmpStr);
   Message(2,TmpStr);
   MessageNewDOE();
  return 0;
}

void MessageCoding(char* name, char* par1) {
   Message(mwMaxStrings-5,name);
   Message(mwMaxStrings-4,par1);
   MessageNewDOE();
   MessageOut();
}

void MessageOutput(char* name, char* par) {
   Message(mwMaxStrings-2,name);
   Message(mwMaxStrings-1,par);
   MessageNewDOE();
   MessageOut();
}
